﻿using System;
using System.IO;
using System.Windows.Forms;
using App.Modelo.Attributes;
using App.Modelo.Menu;
using App.Seguridad.Vista.Menu;

namespace App.Seguridad.Vista.Forms
{
    [Formulario("ConfiguracionPersonal")]
    public partial class ConfigurationForm : Form
    {
        public ConfigurationForm()
        {
            InitializeComponent();
        }

        public string FilePath
        {
            get { return "menu.xml"; }
        }

        public event EventHandler<EventArgs> Guardado;

        private void Configuration_Load(object sender, EventArgs e)
        {
            var menuControlador = new PerfilesMenuControlador(menuStrip1);
            menuControlador.ExecuteCommand += MenuManager_ExecuteCommand;
            menuControlador.Model = new Modelo.Menu.MenuContenedor(new[]
                                                             {
                                                                 new MenuBoton("Guardar", "accept",
                                                                              "ConfiguracionPersonal/modificar"),
                                                                 new MenuBoton("Cancelar", "cancel", "cancel", false)
                                                             });
        }

        private void MenuManager_ExecuteCommand(object sender, MenuEjecutarComandoEventArgs e)
        {
            switch (e.Item.Command)
            {
                case "ConfiguracionPersonal/modificar":
                    guardar();
                    break;
                case "cancel":
                    cerrar();
                    break;
            }
        }

        private void cerrar()
        {
            Hide();
        }

        [Comando("ver")]
        public void modificar()
        {
            TextReader r = new StreamReader(FilePath);
            textBox1.Text = r.ReadToEnd();
            r.Close();

            Show();
        }

        private void guardar()
        {
            TextWriter w = new StreamWriter(FilePath);
            w.Write(textBox1.Text);
            w.Close();

            if (Guardado != null)
                Guardado(this, new EventArgs());
            cerrar();
        }
    }
}